var http = require('http');
var querystring = require('querystring');
const cheerio = require('cheerio')
var request = require('request');
var url = require('url');
var fs = require("fs");
var downImg = require("./down");
var xlsx = require('node-xlsx');

var tableData = xlsx.parse("./aaa.xlsx")
const data = [
  // {
  //   code: 120356,
  //   name: '之江嘉苑2-1-2601'
  // },
  // {
  //   code: 119681,
  //   name: '钱塘名都1-1-1003'
  // }
]
let n = 1;   // 跑第几次任务数
let m = 1000; // 每次跑项目个数
for (var val in tableData) {
  //下标数据
  var itemData = tableData[val];
  //用户表
  if(itemData.name == 'Sheet1'){
      //循环读取用户表数据
      for(var index in itemData.data){
          //0为表头数据
          if(index == 0){
              continue;
          }
          //添加部门表数据
          if((n-1)*m<index&&n*m>=index){
             data.push({
              code:itemData.data[index][0],
              name:itemData.data[index][2], 
            });
          }
         
      }
  }
}
 console.log(data)


const requestUrl = (origin) => {
  console.log(origin)
  var options = {
    hostname: 'sdzsgroup.cn',
    port: 1314,
    path: '/project/project_info_4.aspx?code=' + origin.code,
    method: 'GET',
    headers: {
      Cookie: 'ASP.NET_SessionId=qorrdlmog3ybx4tztl55dokn; UserName=%e6%b2%88%e8%89%b3%e6%a2%85; Usercode=SDKH0009; Demp=HZ3600; DempName=%e6%9d%ad%e5%b7%9e%e5%b7%a5%e7%a8%8b%e7%ae%a1%e7%90%86%e4%b8%ad%e5%bf%83; RoleID=1109; Company=102; Subsid=137; RoleArea=3',
      Referer: 'http://sdzsgroup.cn:1314/project/project_info_4.aspx?code=' + origin.code,
    }
  };
  var req = http.request(options, function (res) {
    res.setEncoding('utf8');
    let html = ''
    res.on('data', function (chunk) {
      html += chunk
    });
    res.on('end',function(){

      // fs.writeFile('./xieru.html',html,function(err){
      //   // 读取失败 err的值也是为空  null转换为布尔值还是false
      //   if (err) {
      //     return console.log(err+'写入失败的');
      //   }
      //   console.log('成绩写入成功');
      // })

      const $ = cheerio.load(html)
      let path = []
      let list2 = []
      let list3 = []
      $('#dl_week_dl_pic_2 img').map((index, item) => {
        if (item.attribs.src.indexOf('WaterY') > -1) {
          path.push(item.attribs.src)
        }  
        let list1 = Array.from(new Set(path))
        list2 = [...new Set(list1)];
      })
      $('#dl_week_dl_pic_ysd_2 img').map((index, item) => {
        if (item.attribs.src.indexOf('WaterY') > -1) {
          path.push(item.attribs.src)
        }  
        let list1 = Array.from(new Set(path))
        list3 = [...new Set(list1)];
      })
      
      list2.map(item => {
        downImg(item,origin)
      })
      list3.map(item => {
        downImg(item,origin)
      })
     
   })
  });
  req.on('error', function (e) {
    console.log('请求失败444 ' + e.message);
  });
  req.end()
}

const sleep = function (ms){
  return new Promise(resolve => setTimeout(resolve, ms))
}


const  FN = async()=>{
  for(let item of data){
    requestUrl(item)
    // await sleep(10000)
  }
}

FN()


//   await Promise.all(
//     data.map(async (item,index)=>{
//       // requestUrl(item)
//       console.log('1111')
//       await sleep(2000)
//       console.log('2222')
//     })
//   );